@include "lib.awk"

function readcsv(w,gp,np,hi,lo,rows,all,d) {
    a(w); a(gp); a(np); a(hi); a(lo); a(all); a(d)
    rows = -1
    while(getline  > 0) {
	rows++
	if (rows == 0) 
            header(w,gp,np,hi,lo,rows,all,d)
	else 
	    row(w,gp,np,hi,lo,rows,all,d) }
}
function header(w,gp,np,hi,lo,rows,all,d,   i) {   
    for(i=1;i<=NF;i++)
	if ($i !~ /\~/) {
	    w[i]  = 1
	    gp[i] = $i ~ /!/
	    np[i] = $i ~ /\$/
	    if (np[i])
		hi[i] = -1* (lo[i]=10^32)}
}
function row(w,gp,np,hi,lo,rows,all,d,   i) {
    all[rows]=rows
    for(i in np) {
	d[rows][i]=$i
	if ($i != "?")
	    if (np[i]) {
		if ($i > hi[i]) hi[i] = $i
		if ($i < lo[i]) lo[i] = $i }}
}
function dumpcsv(gp,all,d,    rows,max,min,col,row,out){
    rows=length(d)
    max = -1 ; min = 10**32
    for(col in gp) {
	if (col > max) max = col
	if (col < min) min = col
    }
    for(row=1;row<=rows;row++) {
	out = d[row][min]
	for(col=min+1;col<=max;col++)
	    if(col in gp) 
		out = out "," d[row][col]
	print "["out"]" }
    print rows,min,max
}